package org.example.demo_ottery.controller.handler;

import org.example.demo_ottery.common.errorcode.GlobalErrorCodeConstants;
import org.example.demo_ottery.common.exception.ControllerException;
import org.example.demo_ottery.common.exception.ServiceException;
import org.example.demo_ottery.common.pojo.CommonResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class GlobalExceptionHandler {
    private final static Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    @ExceptionHandler(value = ServiceException.class)
    public CommonResult<?> serviceException (ServiceException e) {
        log.error("ServiceException:",e);

        return CommonResult.error(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR.getCode()
                , e.getMassage());
    }

    @ExceptionHandler(value = ControllerException.class)
    public CommonResult<?> controllerException (ControllerException e) {
        log.error("ControllerException:",e);

        return CommonResult.error(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR.getCode()
                , e.getMassage());
    }

    @ExceptionHandler(value = Exception.class)
    public CommonResult<?> exception (Exception e) {
        log.error("服务异常:",e);

        return CommonResult.error(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR);
    }
}
